using System;
using System.Collections.Generic;
using System.Text;
using BizTalkCop.Rules.Bases;
using Microsoft.FxCop.Sdk;
using Microsoft.XLANGs.BaseTypes;

namespace BizTalkCop.Rules.Naming.Rules
{
    /// <summary>
    /// Rule verifying property schemas are correctly suffixed "Properties"
    /// </summary>
    public class PropertySchemaNamesShouldHaveCorrectSuffix : ConfiguredRuleBase
    {
        static PropertySchemaNamesShouldHaveCorrectSuffix()
        {
            Dictionary<string, string> defaults = new Dictionary<string, string>();
            defaults.Add(Setting.Suffix, Default.Properties);
            SetDefaults(typeof(PropertySchemaNamesShouldHaveCorrectSuffix), defaults);
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public PropertySchemaNamesShouldHaveCorrectSuffix() : base("PropertySchemaNamesShouldHaveCorrectSuffix") { }

        /// <summary>
        /// Checks if the property schema name is correctly suffixed
        /// </summary>
        /// <param name="type">Type to check</param>
        /// <returns>Problem collection</returns>
        public override ProblemCollection Check(TypeNode type)
        {
            string suffix = this.GetConfiguredSetting(Setting.Suffix);
            if (IsPropertySchema(type) && !type.Name.Name.EndsWith(suffix))
            {
                Problems.Add(new Problem(GetNamedResolution("Suffix", suffix), type));
            }
            return Problems;
        }

        private struct Setting
        {
            public const string Suffix = "Suffix";
        }

        private struct Default
        {
            public const string Properties = "Properties";
        }
    }
}
